package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.annotations.NeedRole;
import com.bwda.dsrs.filecenter.aspect.LogTypeEnum;
import com.bwda.dsrs.filecenter.aspect.Loggable;
import com.bwda.dsrs.filecenter.domain.po.terminaluser.TerminalUserPo;
import com.bwda.dsrs.filecenter.domain.condition.terminaluser.TerminalUserCondition;
import com.bwda.dsrs.filecenter.domain.form.terminaluser.TerminalUserQueryForm;
import com.bwda.dsrs.filecenter.domain.form.terminaluser.TerminalUserUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.terminaluser.TerminalUserCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.terminaluser.TerminalUserVo;
import com.bwda.dsrs.filecenter.service.TerminalUserService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;

import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 终端用户
 * @author yangjun
 */
@RestController
@RequestMapping("/b/terminaluser")
@Api(value = "/b/terminaluser", tags = "终端用户")
public class TerminalUserController extends BaseController {

	@Autowired
	private TerminalUserService terminalUserService;
	@ApiOperation(value = "查询终端用户",notes = "查询终端用户",httpMethod = "GET")
	@Loggable(descp = "查询终端用户",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<TerminalUserVo> query(@ApiParam(value = "ID", required = true)@RequestParam Integer id) throws BusinessServiceException {
		TerminalUserPo po = terminalUserService.queryWithValid(id);
		TerminalUserVo vo = CopyUtil.transfer(po, TerminalUserVo.class);
		return getSuccessResult(vo);
	}
	@ApiOperation(value = "查询终端用户数量",notes = "查询终端用户数量",httpMethod = "GET")
	@Loggable(descp = "查询终端用户数量",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid TerminalUserQueryForm form) throws BusinessServiceException {
		TerminalUserCondition condition = this.getConditionByQueryForm(form);
		int count = terminalUserService.queryCount(condition);
		return getSuccessResult(count);
	}
	@ApiOperation(value = "查询终端用户列表",notes = "查询终端用户列表",httpMethod = "GET")
	@Loggable(descp = "查询终端用户列表",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<TerminalUserVo>> queryList(@ModelAttribute@Valid TerminalUserQueryForm form) throws BusinessServiceException {
		TerminalUserCondition condition = this.getConditionByQueryForm(form);
		List<TerminalUserPo> poList = terminalUserService.queryList(condition);
		List<TerminalUserVo> voList = CopyUtil.transfer(poList, TerminalUserVo.class);
		return getSuccessResult(getListResponse(voList));
	}
	@ApiOperation(value = "查询终端用户列表(带分页)",notes = "查询终端用户列表(带分页)",httpMethod = "GET")
	@Loggable(descp = "查询终端用户列表(带分页)",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<TerminalUserVo>> queryPageList(@ModelAttribute@Valid TerminalUserQueryForm form) throws BusinessServiceException {
		TerminalUserCondition condition = this.getConditionByQueryForm(form);
		List<TerminalUserVo> voList = new ArrayList<>();
		int count = terminalUserService.queryCount(condition);
		if (count > 0) {
			List<TerminalUserPo> poList = terminalUserService.queryList(condition);
			voList = CopyUtil.transfer(poList, TerminalUserVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}
	@ApiOperation(value = "新增终端用户",notes = "新增终端用户",httpMethod = "POST")
	@Loggable(descp = "新增终端用户",type = LogTypeEnum.CREATE,include = "")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<TerminalUserVo> add(@ModelAttribute@Valid TerminalUserCreateForm form) throws BusinessServiceException {
		TerminalUserPo po = CopyUtil.transfer(form, TerminalUserPo.class);
		terminalUserService.insert(po);
		TerminalUserVo vo = CopyUtil.transfer(po, TerminalUserVo.class);
		return getSuccessResult(vo);
	}
	@ApiOperation(value = "修改终端用户",notes = "修改终端用户",httpMethod = "POST")
	@Loggable(descp = "修改终端用户",type = LogTypeEnum.UPDATE,include = "")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid TerminalUserUpdateForm form) throws BusinessServiceException {
		TerminalUserPo po = CopyUtil.transfer(form, TerminalUserPo.class);
		terminalUserService.update(po);
		return getSuccessResult();
	}
	@ApiOperation(value = "删除终端用户",notes = "删除终端用户",httpMethod = "POST")
	@Loggable(descp = "删除终端用户",type = LogTypeEnum.DELETE,include = "")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "ID", required = true)@RequestParam Integer id) throws BusinessServiceException {
		terminalUserService.delete(id);
		return getSuccessResult();
	}

	/**
	 * TerminalUserQueryForm转换为TerminalUserCondition
	 *
	 * @param form
	 * @return
	 */
	private TerminalUserCondition getConditionByQueryForm(TerminalUserQueryForm form) {
		TerminalUserCondition condition = CopyUtil.transfer(form, TerminalUserCondition.class);
		return condition;
	}
}